<?php
/*
 * File name : db_attachCompany.php
* Description : Takes the input from employee and attaches it to the company in the database.
* Created By : Abhiraj Chauhan
*/
include_once "basePage.php";

class dbattachCompany extends basePage{
	public function db_attach_company()
	{
		session_start();
		
		$user_id = $_SESSION['userid'];
		$company_name = $_POST['company_name'];
		$company_zip = $_POST['company_zip'];
		$employee_id = $_POST['employee_id'];
		$match_amount = $_POST['match_amount'];
		$match_percent = $_POST['match_percent'];
		
		$con = mysql_connect('localhost','pa_user','secret_password');
		if (!$con){
			die('Could not connect: ' . mysql_error());
		}
		
		mysql_select_db("TGS_GM_RESERVATION", $con);
			
		$result = mysql_query("SELECT SubscriberID FROM TGS_GM_RESERVATION.tblsubscriber WHERE UserID = '{$user_id}'");
		$row = mysql_fetch_array($result);
		
		$subscriber_id = $row['SubscriberID'];
		
		//echo $subscriber_id;
		
		$result = mysql_query("select CompanyID from tgs_gm_reservation.tblcompany where CompanyName='{$company_name}' AND CompanyZip = '{$company_zip}'");
		$row = mysql_fetch_array($result);
		
		$company_id = $row['CompanyID'];
		
		//echo $company_id;
		
		$result = mysql_query("select CoSubrDetailsID from tgs_gm_reservation.tblcosubrdetails");
		
		$cosubrdetailsid = 0;
		if ($result == FALSE)
			$cosubrdetailsid = 1;
		else{
			while($row = mysql_fetch_array($result))
			{
				$cosubrdetailsid = $row['CoSubrDetailsID'] +1;	
			}
		}
		
		//echo $cosubrdetailsid;
		
		if ($subscriber_id == NULL && $company_id==NULL)
			echo "Error. Please go back and change";
		else{
			$isSet = mysql_query("insert into tblcosubrdetails(CoSubrDetailsID, EmployeeID, CompanyMatchAmount, CompanyMatchPercentage, tblSubscriber_SubscriberID, tblCompany_CompanyID) VALUES ('{$cosubrdetailsid}','{$employee_id}','{$match_amount}','{$match_percent}','{$subscriber_id}', '{$company_id}')");
		}
		
		if ($isSet)
			echo "Updated Successfully";
		else
			echo "Update Not Successful. Please try again.";
				
		mysql_close($con);
	}
}

$page = new dbattachCompany();
$page->header();
$page->include_transalte();
$page->top_menu_donor();
$page->db_attach_company();
$page->footer();
?>